/*****************************************************
PROJECT: BRIDGE C2 SCRAP
JOB:
REQUESTOR:Rachel Gold
JIRA #:
ANALYST: Jean O'Malley
DATE: 9/10/2023/2022
VERSION: 
LAST UPDATED: 
PEER REVIEWER: 

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): 
Rework 
Table 2.Columns Food Insecurity: Need/ No Need/ Not documented
Table 3 Columns Housing Insecurity: Need/ No Need/ Not documented
Table 4 Columns Transportation Insecurity: Need/ No Need/ Not documented
	C.Among Patients due 12 months before obs end, 
		C1.Receipt of any documented screening order within 12 months

	D.Among patients with order>>limit to patients with documented order date
 		D1.probability othe order being FIT/FOBT
 		D2.probability othe order being imaging
		D3: probility of order completion in study period

	E. Among patients with documented FIT/FOBT order: 
		Probability of order completion
	F. Among patients with documented imaging order: 
		Probability of order completion

	

	INPUT DATA:
SCRAP.CRC_outcomes;

OUTPUT DATA:


SECTIONS:
A. Dynamic variables and analysis file

	B.Among Patients due 12 months before obs end, 
		B1.Receipt of any screening order within 12 months

	C.Among patients with order>>limit to patients with documented order date
 		C1.probability othe order being FIT/FOBT
 		C2.probability othe order being imaging
		C3: probility of order completion in study period

	D. Among patients with documented FIT/FOBT order: 
		Probability of order completion
	E. Among patients with documented imaging order: 
		Probability of order completion


****************************************************/
options compress=yes reuse=yes spool;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
proc datasets library=work kill;
/****************************************************/
/***************************************************
A. Dynamic variables and analysis file
***************************************************/




%Let covariates=female race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat;
%Let class=delivery_site_id female age_cat race_ethnic2 language_c  FPL_cat2 visit_cat payor_type_research;

%let SDH=SDH_Food; 
%let SDH=SDH_Housing;
%Let SDH=SDH_Trans;


/********************************************************
/*B.Among Patients due 12 months before obs end, */
*********************************************************/;
data analysis2;
set SCRAP.CRC_analysis_09062023;
where one_year_due^=. and observation_end>=one_year_due;
run;

/*		B1.Receipt of any documented screening order within 12 months*/


	proc genmod data=analysis2 descending;
	class &class &sdh /ref=first;
	model documented_order_1yr=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL diff OM;
	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Documented Order within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Documented Order within 1 year of due";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var4_&sdh; run;

	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;
/*recipt of FITFOBT order*/
	
	proc genmod data=analysis2 descending;
	class &class &sdh /ref=first;
	model documented_FITFOBT_1yr=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL diff OM;
	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Documented RIT Order within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Documented RIT Order within 1 year of due";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var4_&sdh; run;

	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;
/*recipt of Imaging order within one year*/
	proc genmod data=analysis2 descending;
	class &class &sdh /ref=first;
	model documented_imaging_1yr=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL diff OM;
	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Documented Imaging Order within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Documented Imaging Order within 1 year of due";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var4_&sdh; run;

	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;

/******************************************
/*C.Among patients with documented order, */
/******************************************/;
/* 		C1.probability othe order being FIT/FOBT*/

	data analysis3;
	set analysis2;
	where documented_order_1yr=1;
	order_completion=max(completed_FITFOBT_1yr,completed_imaging_1_yr);
	run;
	proc freq data=analysis3;;
	tables &sdh;
	run;
proc freq data=analysis3;
tables documented_FITFOBT_1yr*documented_imaging_1yr/missing;
run;
proc freq data=analysis3;
tables documented_FITFOBT_1yr*documented_imaging_1yr*order_completion/missing;
run;
proc freq data=analysis3;
tables documented_imaging_1yr*imaging_completion_1yr*order_completion/missing;
run;


/* 		D1.probability of the order being FIT/FOBT*/
	/*Food: does not give estimates with random effect*/
	/*Housing: does not give estimates with all covariates*/
	/*Transportation: does not give estimates with all covariates*/
	proc genmod data=analysis3 descending;
	class &class &sdh /ref=first;
	model documented_FITFOBT_1yr=&sdh /*&covariates*/
		/link=log d=binomial type3;

	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=FITorder_&sdh lsmestimates=FITorder_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=FITorder_&sdh out=var5_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var5_&sdh(keep=Outcome &sdh prob_CI);
	set var5_&sdh;
	length Outcome $30;
	Outcome="Probability order was fit FOBT";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=FITorder_diffs_&sdh out=diff5_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff5_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff5_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Probability order was fit FOBT";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var5_&sdh;
	merge var5_&sdh diff5_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var5_&sdh;
	run;

	proc datasets library=work;
	delete FITorder_diffs_&sdh diff5_&sdh  FITorder_&sdh;
	quit;
/* 		C1.probability othe order being imaging*/
	proc genmod data=analysis3 descending;
	class &class &sdh /ref=first;
	model documented_imaging_1yr=&sdh &covariates
		/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=imageorder_&sdh lsmestimates=imageorder_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=imageorder_&sdh out=var6_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var6_&sdh(keep=Outcome &sdh prob_CI);
	set var6_&sdh;
	length Outcome $30;
	Outcome="Probability order was imaging";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=imageorder_diffs_&sdh out=diff6_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff6_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff6_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Probability order was imaging";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var6_&sdh;
	merge var6_&sdh diff6_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var6_&sdh; run;

	proc datasets library=work;
	delete FITorder_diffs_&sdh diff5_&sdh  FITorder_&sdh;
	quit;



/*probability of any order being completed*/
	/*Housing: with all covariates error in computing variance*/
proc freq data=analysis3;
tables order_completion* (female race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat)*&SDH/norow nocol nopercent missing;
run;


	proc genmod data=analysis3 descending;
	class &class &sdh /ref=first;
/*	model order_completion=&sdh &covariates/link=log d=binomial type3;*/
	model order_completion=&sdh female race_ethnic2 /*language_c*/  FPL_cat2 age_cat payor_type_research visit_cat/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=completion_&sdh lsmestimates=completion_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=completion_&sdh out=var7_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var7_&sdh.b(keep=Outcome &sdh prob_CI);
	set var7_&sdh;
	length Outcome $30;
	format mu2 lower2 upper2 4.1;

	Outcome= "Any order completed";
	reference='1_Positive Screen';

	mu2=round(mu*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowermu*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(uppermu*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=completion_diffs_&sdh out=diff7_&sdh (keep= label &SDH probz Mu LowerMu UpperMu);
	by label;
	run;

	data diff7_&sdh.b (keep=Outcome &sdh label reference RR_CI probz);
	set diff7_&sdh;
	length Outcome $30 &sdh reference $20;
    format mu2 lower2 upper2 4.2;

	Outcome="Any order completed";
	reference='1_Positive Screen';
	if label='2 vs 1' then &SDH= '2_Negative Screen';
	if label='3 vs 1' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowermu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(uppermu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	data var7_&sdh;
	merge var7_&sdh.b diff7_&sdh.b;
	by outcome &sdh;
	run;
	proc print data=var7_&sdh;run;

	proc datasets library=work;
	delete completion_diffs_&sdh diff7_&sdh completion_order_&sdh;
	quit;



/*	E. Among patients with documented FIT/FOBT order: */
/*		Probability of order completion*/
Data analysisFIT;
set analysis3;
where documented_FITFOBT_1yr=1;
run;
proc freq data=analysisFIT;
tables &SDH;
run;

	proc genmod data=analysisFIT descending;
	class &class &sdh /ref=first;
	model completed_FITFOBT_1yr=&sdh &covariates
		/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=FITscreen_&sdh lsmestimates=FITscreen_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=FITscreen_&sdh out=var8_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var8_&sdh.b(keep=Outcome &sdh prob_CI);
	set var8_&sdh;
	length Outcome $30;
	Outcome="Probability FITFOBT was completed";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.01);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.01);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.01);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=FITscreen_diffs_&sdh out=diff8_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff8_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff8_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Probability FITFOBT was completed";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.1);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var8_&sdh;
	merge var8_&sdh.b diff8_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var8_&sdh; run;

	proc datasets library=work;
	delete FITscreen_diffs_&sdh diff8_&sdh  FITscreen_&sdh;
	quit;

	
/*	E. Among patients with documented imaging order: */
/*		Probability of order completion*/
Data analysisimage;
set analysis3;
where documented_imaging_1yr=1;
run;
proc freq data=analysisimage;
tables &SDH;
run;

	proc genmod data=analysisimage descending;
	class &class &sdh /ref=first;
	model completed_imaging1yr=&sdh &covariates
		/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=imagescreen_&sdh lsmestimates=imagescreen_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl ilink OM e;
	lsmestimate &sdh '3 vs 2' -1 1 0/cl ilink OM e;
	run;

	proc sort data=imagescreen_&sdh out=var9_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var9_&sdh.b(keep=Outcome &sdh prob_CI);
	set var9_&sdh;
	length Outcome $30;
	Outcome="Probability Imaging was completed";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=Imagescreen_diffs_&sdh out=diff9_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff9_&sdh._b /*(keep=Outcome &sdh reference RR_CI probz)*/;
	set diff9_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Probability Imaging was completed";
	reference='1_Positive Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var9_&sdh;
	merge var9_&sdh.b diff9_&sdh._b;
	by outcome &sdh;
	run;
	proc print data=var9_&sdh; run;

	proc datasets library=work;
	delete imagescreen_diffs_&sdh diff9_&sdh  imagescreen_&sdh;
	quit;

